package com.kotlin.myapplicationd.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;


import com.kotlin.myapplicationd.MApplication;
import com.kotlin.myapplicationd.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/22 0022.
 */
public abstract class BaseFragmentActivity extends FragmentActivity
        implements IBaseActivity {

    /** 日志输出标志 **/
    protected final String TAG = this.getClass().getSimpleName();
    /** 共通操作 **/
    protected Operation mOperation = null;
    protected Resources mRes;
    // -----------------------------------
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    /*** 整个应用Applicaiton **/
    private MApplication mApplication = null;
    /** 当前Activity渲染的视图View **/
    private View mContextView = null;
    /** 动画类型 **/
    private int mAnimationType = NONE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.d(TAG, "BaseActivity-->onCreate()");
        if(isFullScreen())
        {

            requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        }
        //setTranslucentStatus();
        // 获取应用Application
        mApplication = (MApplication) getApplicationContext();

        // 将当前Activity压入栈
        //context = new WeakReference<Activity>(this);
        //mApplication.pushTask(context);
        // 将当前Activity压入栈
        AppManager.getAppManager().addActivity(this);


        mRes = getResources();

        // 实例化共通操作
        mOperation = new Operation(this);

        //mLoadingDialog = new LoadingDialog();


        // 初始化参数
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mAnimationType = bundle.getInt(ANIMATION_TYPE, NONE);
        } else {
            bundle = new Bundle();
        }


        // 设置渲染视图View
        //View mView = bindView();
        //if (null == mView) {
            mContextView = LayoutInflater.from(this)
                    .inflate(bindLayout(), null);
       // } else {
       //     mContextView = mView;
       // }
        setContentView(mContextView);

        //获取上层传递数据
        initParams(bundle);

        // 初始化控件
        initView(mContextView);

        // 业务操作
        doBusiness(this);

        //监听事件
        initListener();

    }

    public View getmContextView() {
        return mContextView;
    }

    /**
     * 抽象类控制是否是全屏显示
     *
     * @return
     */
    public abstract boolean isFullScreen();




    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "BaseActivity-->onRestart()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "BaseActivity-->onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "BaseActivity-->onResume()");
        resume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "BaseActivity-->onPause()");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "BaseActivity-->onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "BaseActivity-->onDestroy()");
        //VolleyUtil.getQueue(context.get()).cancelAll(this);
        //networkHelper.clearQueue();

        destroy();
        //mApplication.removeTask(context);
        //移除任务栈
        //AppManager.getAppManager().removeActivity(this);
        AppManager.getAppManager().finishActivity(this);
    }

    /**
     * 显示Actionbar菜单图标
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        //		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
        //			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
        //				try {
        //					Method m = menu.getClass().getDeclaredMethod(
        //							"setOptionalIconsVisible", Boolean.TYPE);
        //					m.setAccessible(true);
        //					m.invoke(menu, true);// 显示
        //				} catch (Exception e) {
        //					Log.e(TAG, "onMenuOpened-->" + e.getMessage());
        //				}
        //			}
        //		}
        return super.onMenuOpened(featureId, menu);
    }

    /**
     * 显示OverFlowMenu按钮
     *
     * @param mContext
     *            上下文Context
     */
    public void displayOverflowMenu(Context mContext) {
        try {
            ViewConfiguration config = ViewConfiguration.get(mContext);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);// 显示
            }
        } catch (Exception e) {
            Log.e("ActionBar", e.getMessage());
        }
    }

    /**
     * 获取当前Activity
     *
     * @return
     */
    protected Activity getContext() {
        if (null != AppManager.getAppManager().currentActivity())
            return AppManager.getAppManager().currentActivity();
        else
            return null;
    }

    /**
     * 获取共通操作机能
     */
    public Operation getOperation() {
        return this.mOperation;
    }


    //public Map<String, Object> getSystemMap() {
    //
    //    //systemMap.clear();
    //    return this.systemMap;
    //}

    public Map<String, Object> getParamsMap() {

        //paramsMap.clear();
        return this.paramsMap;
    }



    //public void setmLoadingDialog(LoadingDialog mLoadingDialog) {
    //    this.mLoadingDialog = mLoadingDialog;
    //}

//    public void setLoadingMsg(String msg){
//        this.mLoadingDialog.setMsg(msg);
//    }

    /**
     * Actionbar点击返回键关闭事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //		case android.R.id.home:
            //			finish();
            //			break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void finish() {
        super.finish();
        switch (mAnimationType) {
            case IBaseActivity.LEFT_RIGHT:
                overridePendingTransition(0, R.anim.base_slide_right_out);
                break;
            case IBaseActivity.TOP_BOTTOM:
                overridePendingTransition(0, R.anim.push_up_out);
                break;
            case IBaseActivity.FADE_IN_OUT:
                overridePendingTransition(0, R.anim.fade_out);
                break;
            default:
                break;
        }
        mAnimationType = NONE;
    }


    /**
     * 判断	服务器返回数据是否为空
     *
     *
     * @return
     */
    public boolean isServicesDataNull(JSONObject data)//
    {

        try {
            if(data.has("data") && data.getString("data").equals("null")) {
                return true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;

    }

    public MApplication getmApplication() {
        return mApplication;
    }

}
